<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="950px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @deleteCancel="deleteCancel" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">服务登记</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="12">
            <el-form-item label="服务单号" prop="serviceNo">
              <el-input v-model="ruleForm.serviceNo" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务客户" prop="customer">
              <el-input v-model="ruleForm.customer" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务类型" prop="type">
              <el-input v-model="ruleForm.type" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务项" prop="item">
              <el-input v-model="ruleForm.item" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务内容" prop="content">
              <el-input v-model="ruleForm.content" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主负责人" prop="userId">
              <el-select v-model="ruleForm.userId" style="width: 100%">
                <el-option v-for="item in userAllList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="beginDate" label-width="80px">
              <el-date-picker v-model="ruleForm.beginDate" :clearable="false" format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期时间" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endDate" label-width="80px">
              <el-date-picker v-model="ruleForm.endDate" :clearable="false" format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期时间" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总计周期" prop="totalCycle">
              <el-input v-model="ruleForm.totalCycle" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务情况" prop="situation">
              <el-input v-model="ruleForm.situation" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否完工" prop="isFinish">
              <el-input v-model="ruleForm.isFinish" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="ruleForm.remark" maxlength="200" type="textarea" rows="3" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
  </el-dialog>
</template>
<script>
import bottomFont from '@/components/BottomFont'
export default {
  components: { bottomFont },
  props: {
    userAllList: {
      type: Array,
      default: () => {
        return {}
      }
    }
  },
  data() {
    const checkServiceNo = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/crm/register/verifyUnique?serviceNo=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('服务单号重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      ruleForm: {
        serviceNo: '',
        content: '',
        beginDate: '',
        endDate: '',
        situation: '',
        type: '',
        totalCycle: '',
        customer: '',
        item: '',
        isFinish: '',
        userId: ''
      },
      rules: {
        serviceNo: [
          { required: true, message: '服务单号不能为空！', trigger: 'blur' },
          { validator: checkServiceNo, trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      customerAllList: [],
      obj: {}
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.obj = row
      // this.getCustomerData()
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
      this.dialogVisible = true
    },
    /**
     * @description: 获取所有启用的客户信息
     * @return {*}
     */
    async getCustomerData() {
      const res = await this.$axios.get('/basic/customer/allBySave')
      this.customerAllList = [{ id: '0', customerName: '无' }, ...res.data.data]
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/crm/register/save',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    async deleteCancel() {
      if (this.title === '编辑') {
        this.$confirm(`确认删除数据？`, '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$axios
              .delete(`/crm/register/remove/${this.obj.id}`)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$notify.success('删除成功！')
                  this.dialogVisible = false
                  this.$emit('getTableInfo', 1)
                } else {
                  this.$notify.error(res.data.msg)
                }
              })
          })
          .catch(() => {})
      }
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = Object.assign({}, this.$options.data.call(this).ruleForm)
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
